package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author ZJ
 */
@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ListenPoint(schema = "changgou_business", table = {"tb_ad"})
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("广告数据发生变化");

//        //修改前数据
    //   List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
       //        for (CanalEntry.Column column : beforeColumnsList) {
//            System.out.println("改变前的列名:"+column.getName());
//
//            System.out.println("值是:"+column.getValue());
//
//        }
         //监听到tb_ad有改动了
        // 向mq发消息  queuqe: ad_update_queue         内容:position
      //  rabbitTemplate.convertAndSend("ad_update_queue","");


        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
           if(column.getName().equals("position")){
               String value=column.getValue();
               // 向mq发消息  queuqe: ad_update_queue         内容:position
             //  rabbitTemplate.convertAndSend("ad_update_queue","");
               rabbitTemplate.convertAndSend("","ad_update_queue",value);
           }

            System.out.println("改变后的列名:"+column.getName());
            System.out.println("z值是:"+column.getValue());
        }
//
//        //修改后数据
//        for(CanalEntry.Column column: rowData.getAfterColumnsList()) {
//            if(column.getName().equals("position")){
//                System.out.println("发送消息到mq  ad_update_queue:"+column.getValue());
//                rabbitTemplate.convertAndSend("","ad_update_queue",column.getValue());  //发送消息到mq
//                break;
//            }
//        }
    }
}
